package cn.fresh.xuanyuan.module.trade.controller.admin.order.vo;

import cn.fresh.xuanyuan.framework.common.util.json.JsonUtils;
import cn.fresh.xuanyuan.module.trade.controller.admin.base.product.property.ProductPropertyValueDetailRespVO;
import cn.fresh.xuanyuan.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.fresh.xuanyuan.module.trade.enums.order.TradeOrderRefundStatusEnum;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 交易订单项 Base VO，提供给添加、修改、详细的子 VO 使用
 * 如果子 VO 存在差异的字段，请不要添加到这里，影响 Swagger 文档生成
 */
@Data
public class TradeOrderItemBaseVO {

    // ========== 订单项基本信息 ==========

    @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long id;

    @Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long userId;

    @Schema(description = "订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long orderId;

    // ========== 商品基本信息 ==========

    @Schema(description = "商品 SPU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long spuId;

    @Schema(description = "商品 SPU 名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道源码")
    private String spuName;

    @Schema(description = "商品 SKU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long skuId;

    @Schema(description = "商品图片", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/1.png")
    private String picUrl;

    @Schema(description = "购买数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer count;

    // ========== 价格 + 支付基本信息 ==========

    @Schema(description = "商品原价（单）", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer price;

    @Schema(description = "商品优惠（总）", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer discountPrice;

    @Schema(description = "商品实付金额（总）", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer payPrice;

    @Schema(description = "子订单分摊金额（总）", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer orderPartPrice;

    @Schema(description = "分摊后子订单实付金额（总）", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer orderDividePrice;

    /**
     * 发货时间
     */
    @Schema(description = "下单时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2024-06-01 01：00:00")
    private LocalDateTime createTime;

    // ========== 营销基本信息 ==========

    // TODO 芋艿：在捉摸一下

    // ========== 售后基本信息 ==========

    @Schema(description = "售后状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer afterSaleStatus;

    @Schema(description = "订单号", requiredMode = Schema.RequiredMode.REQUIRED, example = "B00041141")
    private String no;

    @Schema(description = "子单号", requiredMode = Schema.RequiredMode.REQUIRED, example = "dfdfdf1")
    private String orderItemNo;

    /**
     * 发货物流单号
     *
     * 如果无需发货，则 logisticsNo 设置 ""。原因是，不想再添加额外字段
     */
    @Schema(description = "快递单号", requiredMode = Schema.RequiredMode.REQUIRED, example = "SF0454dfdf1")
    private String logisticsNo;
    /**
     * 发货时间
     */
    @Schema(description = "发货时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2024-06-01 01：00:00")
    private LocalDateTime deliveryTime;

    /**
     * 退款状态
     * 枚举 {@link TradeOrderRefundStatusEnum}
     */
    @Schema(description = "退款状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer refundStatus;

    /**
     * 快递公司名称
     */
    @Schema(description = "快递名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "圆通")
    private String expressName;

    @Schema(description = "收货时间")
    private LocalDateTime receiveTime;

    @Schema(description = "收件人名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "张三")
    private String receiverName;

    @Schema(description = "收件人手机", requiredMode = Schema.RequiredMode.REQUIRED, example = "13800138000")
    private String receiverMobile;

    @Schema(description = "收件人地区编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "110000")
    private Integer receiverAreaId;

    @Schema(description = "收件人详细地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "中关村大街 1 号")
    private String receiverDetailAddress;

    @Schema(description = "备注", requiredMode = Schema.RequiredMode.REQUIRED, example = "不要打电话")
    private String remark;

    @Schema(description = "订单状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Integer status;

    /**
     * 果园ID
     */
    @Schema(description = "果园ID")
    private Integer orchardId;

    /**
     * 果园名称
     */
    @Schema(description = "果园名称")
    private String orchardName;


    private List<ProductPropertyValueDetailRespVO> properties;


    public static class PropertyTypeHandler extends AbstractJsonTypeHandler<List<ProductPropertyValueDetailRespVO>> {

        @Override
        protected List<ProductPropertyValueDetailRespVO> parse(String json) {
            return JsonUtils.parseArray(json, ProductPropertyValueDetailRespVO.class);
        }

        @Override
        protected String toJson(List<ProductPropertyValueDetailRespVO> obj) {
            return JsonUtils.toJsonString(obj);
        }

    }

}
